//
//  CPNewController.m
//  News
//
//  Created by chen on 15/3/24.
//  Copyright (c) 2015年 chen. All rights reserved.
//

#import "CPNewController.h"
#import "CPWaterflowLayout.h"
#import "CPNewCell.h"
#import "CPNewModal.h"
#import "CPCellModal.h"
#import "CPNewDetaieController.h"
#import "NSString+CPString.h"
#import "MJRefresh.h"

static CGFloat imageH = 60.f;
@interface CPNewController ()<waterFlowLayoutDelegate>
@property (nonatomic, strong) NSArray *modals;

@end

@implementation CPNewController

static NSString * const reuseIdentifier = @"NewCell";

// homeModal的高度
- (void)setHomeModal:(CPCellModal *)homeModal{
    _homeModal = homeModal;
    [CPNewModal loadModal:homeModal.URLString completion:^(NSArray *array, NSError *error) {
        if (error != nil) {
            NSLog(@"%@",error);
        }
        self.modals = array;
        
    }];
}

// setModal 中的 结果
- (void)setModals:(NSArray *)modals{
    _modals = modals;
    UIFont *titleFont = [UIFont systemFontOfSize:17.0];
    UIFont *digestFont = [UIFont systemFontOfSize:15.0];
    CGSize size = CGSizeMake(100.0f, 500.0f);
    
    for (CPNewModal *modal in modals) {
        
        [self setUPModals:modal titleFont:titleFont digestFont:digestFont size:(CGSize)size];
    }
    
    [self.collectionView reloadData];
}

// 设置Modal的高度
- (void)setUPModals:(CPNewModal *)modal titleFont:(UIFont *)tFont digestFont:(UIFont *) dFont size:(CGSize)size{
    
    CGFloat titleH = [modal.title  heightWithFont:tFont size:size];
    CGFloat digestH = [modal.digest heightWithFont:dFont size:size];
    
    modal.cellHeight = titleH + digestH + imageH + 30.f;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUPView];
    
    [self setRefresh];
}


// 设置刷新
- (void)setRefresh{
    

    [self.collectionView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(header)];
    
    [self.collectionView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(footer)];
}

// 头部刷新事情
- (void)headerRefresh{
    
    
    
}

// 尾部刷新事件
- (void)footerRefresh{
    
}

// 设置布局
- (void)setUPView{
    
    CPWaterflowLayout *waterLayout =[[CPWaterflowLayout alloc] init];
    waterLayout.delegate = self;
    self.collectionView.collectionViewLayout = waterLayout;
     self.collectionView.backgroundColor = [UIColor colorWithRed:244/256.0 green:244/256.0 blue:244/256.0 alpha:1.0];
    
}

// 生成控制器对象
+ (CPNewController *)newController{
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"New" bundle:nil];
    
    return  [sb instantiateInitialViewController];
}

#pragma mark -waterFlowLayoutDelegate 代理方法

// 先调用 高度，要有cell 才行：
- (CGFloat)waterFlowLayout:(CPWaterflowLayout *)layout heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CPNewModal *modal = self.modals[indexPath.row];
    return modal.cellHeight;
    
}

#pragma  mark  - UICollectionView 的代理方法


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return  self.modals.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CPNewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.modal = self.modals[indexPath.row];

    return  cell;
}


// 点击哪个Cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CPNewDetaieController *detailVC = [CPNewDetaieController controller];
    
    detailVC.NewsModal = self.modals[indexPath.row];
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)dealloc{
    NSLog(@"内存释放");
}


@end
